package com.heima.admin.service.impl;

import com.heima.admin.mapper.AdminChannelMapper;
import com.heima.admin.service.IAdminChannelService;
import com.heima.api.IArticleClient;
import com.heima.api.admin.IAdminClient;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AdminChannelServiceImpl implements IAdminChannelService {

    @Autowired
    private IAdminClient adminClient;
    @Autowired
    private IArticleClient articleClient;


    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //只有禁用的频道才能删除
        WmChannel wmChannel = adminClient.queryById(id);
        if (!wmChannel.getStatus()) {
            adminClient.delChannel(id);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }
        return ResponseResult.okResult("删除成功");
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //如果频道被引用则不能被禁用
        //需要到Article表中查询频道id是否存在
        List<Integer> channelIds = articleClient.queryArticleId();
        if (!channelIds.contains(wmChannel.getId())){
            adminClient.updateChannel(wmChannel);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        return ResponseResult.okResult("修改成功");
    }
}
